package com.shop2cn.iapi.agentproducts.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.MqDeleteProductBean;
import com.shop2cn.iapi.agentproducts.service.MqDeleteProductCall;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;

/**
 * 删除商品MQ回调Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_MqDeleteProduct {
	private static MqDeleteProductBean mqdeleteproductBean;
	private static MqDeleteProductCall mqdeleteproductCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("删除商品MQ回调Test");
	}

	@Before
	public void caseUp() {
		mqdeleteproductBean = new MqDeleteProductBean();
		mqdeleteproductCall = new MqDeleteProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mqdeleteproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	// vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";

	private YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_MqDeleteProduct_001() {
		Logger.start(true, "渠道商删除商品，同时删除下级");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId, "",
					true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, "", productId, vipRootUserId, false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId2, "", agentProductId, vipAgentUserId1, false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);

			mqdeleteproductBean.setProductId(productId);
			mqdeleteproductBean.setRemoveChildProduct(true);
			mqdeleteproductCall.setData(mqdeleteproductBean);
			mqdeleteproductCall.callService();
			Logger.verifyEquals("ok", mqdeleteproductCall.getReturnData(), "验证code");

			// 查询代理商的商品id
			List<AgentProductPo> ap = ymtProductsIWapper.selectAgentProductByRootProductId(productId).stream()
					.filter(x -> x.getUserId() == vipAgentUserId1).collect(Collectors.toList());
			Logger.verifyEquals(-1, ap.get(0).getAction(), "代理1商品被删除");

			// 查询代理商的商品id
			List<AgentProductPo> ap1 = ymtProductsIWapper.selectAgentProductByRootProductId(productId).stream()
					.filter(x -> x.getUserId() == vipAgentUserId2).collect(Collectors.toList());
			Logger.verifyEquals(-1, ap1.get(0).getAction(), "代理2商品被删除");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}